/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.partnerapply;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.core.plugins.wxmsg.WxMsgUtil;
import shop.core.util.KeyValueHelper;
import shop.core.Services;
import shop.core.util.MD5;
import shop.services.manage.partnerapply.PartnerapplyService;
import shop.services.manage.partnerapply.bean.Partnerapply;
import shop.core.BaseController;
import shop.core.RequestHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**   
 * @类名称：PartnerapplyAction      
 * @创建人：wzl
 * @创建时间：2018-01-03 下午16:39:49      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/partnerapply/")
public class PartnerapplyAction extends BaseController<Partnerapply> {
    private static final Logger logger = LoggerFactory.getLogger(PartnerapplyAction.class);
    private final  static  String PARTNERAPPLY_STATUS_PASS="pass";
    private final  static  String USER_RID_PARTNER="2";
    private final  static  String USER_PID_NONE="0";
    @Autowired
    private PartnerapplyService partnerapplyService;
    @Autowired
    private UserService userService;

    private static final String page_toList = "/manage/partnerapply/partnerapplyList";
    public PartnerapplyService getService() {
        return partnerapplyService;
    }

    private PartnerapplyAction(){
        super.page_toList = page_toList;
        super.page_toAdd = null;
        super.page_toEdit = null;
    }

    /**
     * 查询股东申请列表
     *
     * @param request 请求
     * @param partnerapply       对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectPartnerApplyPage")
    @ResponseBody
    public PagerModel selectPartnerApplyPage(HttpServletRequest request, Partnerapply partnerapply) {
        int offset = 0;
        int pageSize = 10;
        int start=Integer.parseInt(request.getParameter("start"));
        int length= Integer.parseInt(request.getParameter("length"));
        if(start>0)
            offset=start;
        if(length>0)
            pageSize=length;
        partnerapply.setOffset(offset);
        partnerapply.setPageSize(pageSize);
        PagerModel pager = getService().selectPartnerApplyPage(partnerapply);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }


    /**
     * 审批股东申请
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "updatePartnerApply", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updatePartnerApply(Partnerapply partnerapply) throws Exception {
        jsonResult = new JSONResult();
        User user=new User();
        user.setId(partnerapply.getUserID());
        user=userService.selectOne(user);
        partnerapplyService.update(partnerapply);
        //股东申请消息通知
        Map<String, String> msgParam = new HashMap<>();
        msgParam.put("first", "尊敬的用户，您的股东申请审核结果如下：");
        msgParam.put("keyword1", "申请成为味爱地图股东");
        if(PARTNERAPPLY_STATUS_PASS.equals(partnerapply.getStatus())){
            msgParam.put("keyword2","通过" );
        }else{
            msgParam.put("keyword2","不通过" );
        }
        msgParam.put("remark", "感谢您的参与！");
        WxMsgUtil.sendPartnerCheckStatusMsg(msgParam, systemManager, user.getOpenID());
        String pid=user.getPid();
        if(PARTNERAPPLY_STATUS_PASS.equals(partnerapply.getStatus())){
            user.setRid(USER_RID_PARTNER);
            user.setPid(USER_PID_NONE);
            userService.update(user);
            //下级解绑模板消息推送
            if(!"0".equals(pid)) {
                User subUser = new User();
                subUser.setId(pid);
                subUser = userService.selectOne(subUser);
                Map<String, String> msgParam2 = new HashMap<>();
                msgParam2.put("first", "您好，您有一个下家解绑关系！");
                msgParam2.put("keyword1", user.getNickname());
                msgParam2.put("keyword2", "会员");
                msgParam2.put("keyword3", user.getUpdateTime());
                msgParam2.put("remark", "请注意查看");
                WxMsgUtil.sendUnBindMsg(msgParam2, systemManager, subUser.getOpenID());
            }
        }
        return jsonResult;
    }

}
